//
//  ActivityCell.m
//  Contacts
//
//  Created by 李 雷川 on 14-7-19.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "ActivityCell.h"
#import "DateMethod.h"
@interface ActivityCell ()

@property (nonatomic, assign) BOOL didSetupConstraints;

@end

@implementation ActivityCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
       
        [self setupSubViews];
      
 
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code

    [headImageView setContentMode:UIViewContentModeScaleAspectFill];
    [headImageView setClipsToBounds:YES];

    [headImageView.layer setCornerRadius:headImageView.frame.size.width/2.0];
    
    nameLabel.adjustsFontSizeToFitWidth= YES;
    joinNumLabel.adjustsFontSizeToFitWidth = YES;
    [subjectLabel sizeToFit];
    [detailLabel sizeToFit];
    
  
    
    //[self setupSubViews];
    
}

-(void)layoutSubviews{

    
    float margin = 10;
    float paddingTop =20;
    float paddingLeft =20;
    float innerSpace = 5;
    float topHeigth =25;
    float areaSpace = 10;
    
    bgImageView.frame = CGRectMake(margin,margin,CGRectGetWidth(self.frame)- margin * 2, CGRectGetHeight(self.frame) - 2 * margin);
    
    float headWidth = 48;
    float headHeight = 48;
    headImageView.frame = CGRectMake(4,4,headWidth,headHeight);
    [headImageView setClipsToBounds:YES];
    [headImageView.layer setCornerRadius:headImageView.frame.size.width/2.0];
    
    CGSize size = [nameLabel sizeThatFits:CGSizeMake(100,topHeigth)];
    nameLabel.frame =CGRectMake(CGRectGetMaxX(headImageView.frame) + innerSpace,paddingTop,size.width,topHeigth);
    
    timeLabel.frame =CGRectMake(CGRectGetMaxX(nameLabel.frame)+innerSpace,paddingTop,100,topHeigth);
    
    
    CGSize joinNumSize = [nameLabel sizeThatFits:CGSizeMake(100,topHeigth)];
    joinNumLabel.frame =CGRectMake(CGRectGetWidth(self.contentView.frame) - 40 - innerSpace,paddingTop,20,topHeigth);
    
    joinBtn.frame =CGRectMake(CGRectGetMinX(joinNumLabel.frame) - innerSpace - joinNumSize.width,paddingTop,joinNumSize.width,topHeigth);
    
    lineImageView.frame = CGRectMake(CGRectGetMaxX(headImageView.frame),CGRectGetMaxY(nameLabel.frame) + innerSpace,CGRectGetWidth(bgImageView.frame) - CGRectGetMaxX(headImageView.frame) - innerSpace, 1);
    
    
    subjectLabel.frame = CGRectMake(paddingLeft,CGRectGetMaxY(lineImageView.frame) + areaSpace,CGRectGetWidth(bgImageView.frame) - 2 * paddingLeft,title_font);
    [subjectLabel sizeToFit];
    
    timeLogoImageView.frame = CGRectMake(paddingLeft,CGRectGetMaxY(subjectLabel.frame) + areaSpace + 2, 13,13);
    
    timeTitleLabel.frame = CGRectMake(CGRectGetMaxX(timeLogoImageView.frame) +innerSpace ,CGRectGetMaxY(subjectLabel.frame) + areaSpace,70,content_font);
    [timeTitleLabel sizeToFit];

    
    activityTimeLabel.frame = CGRectMake(CGRectGetMaxX(timeTitleLabel.frame) + innerSpace,CGRectGetMaxY(subjectLabel.frame) + areaSpace, 70,content_font);
    [activityTimeLabel sizeToFit];
    
    attentionBtn.frame = CGRectMake(CGRectGetWidth(bgImageView.frame) - 85,CGRectGetMaxY(subjectLabel.frame) + 8, 75,22);
    
    
    placeLogoImageView.frame = CGRectMake(paddingLeft,CGRectGetMaxY(timeLogoImageView.frame) + innerSpace + 3, 13,13);
    
    placeTitleLabel.frame = CGRectMake(CGRectGetMaxX(placeLogoImageView.frame) + innerSpace,CGRectGetMaxY(timeTitleLabel.frame) + innerSpace,70,content_font);
    [placeTitleLabel sizeToFit];
    
    placeLabel.frame = CGRectMake(CGRectGetMaxX(placeTitleLabel.frame)+innerSpace ,CGRectGetMaxY(activityTimeLabel.frame) + 5, 70,content_font);
    [placeLabel sizeToFit];
    
    detailLabel.frame = CGRectMake(paddingLeft,CGRectGetMaxY(placeTitleLabel.frame) + areaSpace/2,CGRectGetWidth(bgImageView.frame) - paddingLeft * 2,description_font);
    [detailLabel sizeToFit];
}

-(void)setupSubViews{
//    self.contentView.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.1];
    
    bgImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    bgImageView.userInteractionEnabled = YES;
    bgImageView.image = [[UIImage imageNamed:@"activit_bg.png"]stretchableImageWithLeftCapWidth:50 topCapHeight:50];
    
    headImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    headImageView.image = [UIImage imageNamed:@"logo.png"];
    
    nameLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    [nameLabel setNumberOfLines:1];
    nameLabel.font =[UIFont systemFontOfSize:content_font];
    [nameLabel setTextAlignment:NSTextAlignmentLeft];
    [nameLabel setBackgroundColor:[UIColor clearColor]];
    
    timeLabel =  [[UILabel alloc]initWithFrame:CGRectZero];;
    [timeLabel setNumberOfLines:1];
    timeLabel.font =[UIFont systemFontOfSize:description_font];
    [timeLabel setTextAlignment:NSTextAlignmentLeft];
    [timeLabel setTextColor:[UIColor lightGrayColor]];
    [timeLabel setBackgroundColor:[UIColor clearColor]];
    
    joinNumLabel =  [[UILabel alloc]initWithFrame:CGRectZero];;
    [joinNumLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [joinNumLabel setNumberOfLines:1];
    joinNumLabel.font =[UIFont systemFontOfSize:content_font];
    [joinNumLabel setTextAlignment:NSTextAlignmentLeft];
    [joinNumLabel setTextColor:[UIColor redColor]];
    [joinNumLabel setBackgroundColor:[UIColor clearColor]];
    
    joinBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [joinBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
    [joinBtn setImage:[UIImage imageNamed:@"join_activity.png"] forState:UIControlStateNormal];
    
    lineImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    lineImageView.image = [UIImage imageNamed:@"dot_line.png"];
    
    subjectLabel = [[UILabel alloc]initWithFrame:CGRectZero];;
    [subjectLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [subjectLabel setNumberOfLines:0];
    subjectLabel.font =[UIFont systemFontOfSize:title_font];
    [subjectLabel setTextAlignment:NSTextAlignmentLeft];
    [subjectLabel setBackgroundColor:[UIColor clearColor]];
    
    timeLogoImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    [timeLogoImageView setContentMode:UIViewContentModeCenter];
    timeLogoImageView.image = [UIImage imageNamed:@"time_icon.png"];
    
    timeTitleLabel =  [[UILabel alloc]initWithFrame:CGRectZero];;
    [timeTitleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [timeTitleLabel setNumberOfLines:1];
    timeTitleLabel.font =[UIFont systemFontOfSize:content_font];
    [timeTitleLabel setTextAlignment:NSTextAlignmentLeft];
    [timeTitleLabel setTextColor:[UIColor blackColor]];
    [timeTitleLabel setBackgroundColor:[UIColor clearColor]];
    timeTitleLabel.text = @"参加时间:";
    
    activityTimeLabel =  [[UILabel alloc]initWithFrame:CGRectZero];;
    [activityTimeLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [activityTimeLabel setNumberOfLines:1];
    activityTimeLabel.font =[UIFont systemFontOfSize:content_font];
    [activityTimeLabel setTextAlignment:NSTextAlignmentLeft];
    [activityTimeLabel setBackgroundColor:[UIColor clearColor]];
    
    placeLogoImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    [placeLogoImageView setContentMode:UIViewContentModeCenter];
    placeLogoImageView.image = [UIImage imageNamed:@"place_icon.png"];
    
    placeTitleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    [placeTitleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [placeTitleLabel setNumberOfLines:1];
    placeTitleLabel.font =[UIFont systemFontOfSize:content_font];
    [placeTitleLabel setTextAlignment:NSTextAlignmentLeft];
    [placeTitleLabel setBackgroundColor:[UIColor clearColor]];
    placeTitleLabel.text = @"参加地点:";
    
    placeLabel =  [[UILabel alloc]initWithFrame:CGRectZero];;
    [placeLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [placeLabel setNumberOfLines:1];
    placeLabel.font =[UIFont systemFontOfSize:content_font];
    [placeLabel setTextAlignment:NSTextAlignmentLeft];
    [placeLabel setBackgroundColor:[UIColor clearColor]];
    
    
    detailLabel =  [[UILabel alloc]initWithFrame:CGRectZero];;
    [detailLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [detailLabel setNumberOfLines:0];
    detailLabel.font =[UIFont systemFontOfSize:description_font];
    [detailLabel setTextAlignment:NSTextAlignmentLeft];
    [detailLabel setTextColor:[UIColor darkGrayColor]];
    [detailLabel setBackgroundColor:[UIColor clearColor]];
    
    attentionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [attentionBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
    [attentionBtn setImage:[UIImage imageNamed:@"attention.png"] forState:UIControlStateNormal];
    
    [self.contentView addSubview:bgImageView];
    [bgImageView addSubview:headImageView];
    [bgImageView addSubview:nameLabel];
    [bgImageView addSubview:timeLabel];
    [bgImageView addSubview:lineImageView];
    
    [bgImageView addSubview:joinNumLabel];
    [bgImageView addSubview:joinBtn];
    
    [bgImageView addSubview:subjectLabel];

    [bgImageView addSubview:timeTitleLabel];
    [bgImageView addSubview:activityTimeLabel];
    [bgImageView addSubview:attentionBtn];
    [bgImageView addSubview:placeTitleLabel];
    [bgImageView addSubview:placeLabel];
    [bgImageView addSubview:detailLabel];
    [bgImageView addSubview:timeLogoImageView];
    [bgImageView addSubview:placeLogoImageView];
    
    

    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}




-(void)updateFrame{
   
    

    
    
}

//- (void)layoutSubviews
//{
//    [super layoutSubviews];
//    
//    // The below call to layoutSubviews on the table view cell's contentView should NOT be necessary.
//    // However, in some (but not all!) cases it appears as though the super implementation does not call
//    // layoutSubviews on the contentView, which causes all the UILabels to have a frame of CGRectZero.
//    [self.contentView layoutSubviews];
//    
//    // Set the preferredMaxLayoutWidth of the mutli-line bodyLabel based on the evaluated width of the label's frame,
//    // as this will allow the text to wrap correctly, and as a result allow the label to take on the correct height.
//    subjectLabel.preferredMaxLayoutWidth = CGRectGetWidth(subjectLabel.frame);
//}

- (void)updateFonts
{
    nameLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
    subjectLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2];
    timeLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
}


-(void)initWithActivity:(Activity *)_activity{
    activity = _activity;
    if (activity) {

        nameLabel.text = activity.initiator.name;
 
        joinNumLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)activity.joinArray.count];

        subjectLabel.text = activity.subject;
   
        //获取当前那本属性

//        NSAttributedString *attStr = [[NSAttributedString alloc]initWithString:activity.subject];
//        NSRange range =NSMakeRange(0,attStr.length );
//        NSDictionary *dic = [attStr attributesAtIndex:0 effectiveRange:&range];//获取该段attstr的属性字典
//        //计算实际frame大小，并将label的frame变成实际大小
//        CGSize subjectLabelSize = [activity.subject boundingRectWithSize:CGSizeMake(250, 999) options:NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
//        CGRect subjectFrame = subjectLabel.frame;
//        subjectLabel.frame = CGRectMake(subjectFrame.origin.x,subjectFrame.origin.y, subjectLabelSize.width,subjectLabelSize.height);
        
        
        timeLabel.text = [DateMethod timestampFromString:activity.time];
        activityTimeLabel.text = [NSString stringWithFormat:@"%@-%@",[DateMethod changeDateTohhmmString:activity.beginDate],[DateMethod changeDateTohhmmString:activity.endDate]];
        placeLabel.text = activity.place;
        subjectLabel.text = activity.subject;
        detailLabel.text =[NSString stringWithFormat:@"详情:%@",activity.content] ;
        
        }
}

@end
